<template xmlns:v-slot="http://www.w3.org/1999/XSL/Transform">
    <div class="goods">
      <!--添加商品-->
      <el-button @click="goAdd" type="success" plain>添加商品</el-button>
      <br><br>

      <!--表格-->
      <el-table
        v-loading="isLoading"
        :data="categoryList"
        row-key="cat_id">

        <el-table-column label="#" :index="indexMethod" type="index"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column label="商品价格" prop="goods_price"></el-table-column>
        <el-table-column label="商品重量" prop="goods_weight"></el-table-column>
        <el-table-column label="创建时间">
          <template v-slot="{ row }">
            {{ row.add_time | time }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot:default="{ row }">
            <el-button size="small" plain type="primary" icon="el-icon-edit"></el-button>
            <el-button size="small" plain type="danger" icon="el-icon-delete"></el-button>
          </template>
        </el-table-column>

      </el-table>

      <!--分页-->
      <br>
      <div class="block">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pagenum"
          :page-sizes="[8, 16, 24, 32]"
          :page-size="pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>

    </div>
</template>

<script>
export default {
  data () {
    return {
      // 当前页
      pagenum: 1,
      // 一页多少条
      pagesize: 8,
      // 后台返回的总数据
      categoryList: [],
      Pnp: '',
      // 记录一个多少条
      total: 0,
      // 加载动画
      isLoading: false
    }
  },
  created () {
    this.getCategoryList()
  },
  methods: {
    // 封装渲染函数
    async getCategoryList () {
      // 加载动画显示
      this.isLoading = true
      const res = await this.$axios.get(`goods`, {
        params: {
          Pnp: this.Pnp,
          pagenum: this.pagenum,
          pagesize: this.pagesize
        }
      })
      const { data, meta } = res
      if (meta.status === 200) {
        // 保存后台返回的数据
        this.categoryList = data.goods
        // 记录一个多少条信息
        this.total = data.total
      } else {
        this.$message.error(meta.msg)
      }
      // 加载动画隐藏
      this.isLoading = false
    },

    // 分页
    handleSizeChange (val) {
      this.pagesize = val
      this.pagenum = 1
      this.getCategoryList()
    },
    handleCurrentChange (val) {
      this.pagenum = val
      this.getCategoryList()
    },

    // 分页下标问题
    indexMethod (index) {
      return (this.pagenum - 1) * this.pagesize + index + 1
    },

    // 去到添加商品组件
    goAdd () {
      // 跳转
      this.$router.push({ path: '/goods-add' })
    }

  }
}
</script>

<style scoped lang="less">

  .demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }

</style>
